<!DOCTYPE html>
<html lang="en">
<title>权限中心-用户管理</title>
<#import "/layout/layout.html" as body/>
<@body.page>

<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5 class="pull-left">用户列表</h5>
                </div>
                <div class="ibox-content">
                    <div class="table-responsive">
                        <form class="form-inline" role="form">
                            <div class="form-group">
                                <label for="storeName">用户名：</label>
                                <input class="form-control" type="text" id="nickName" name="nickName"/>
                            </div>
                            <div class="form-group">
                                <label for="storeName1">手机号：</label>
                                <input class="form-control" type="text" id="mobile" name="mobile"/>
                            </div>
                            <div class="form-group">
                                <label>用户类型：</label>
                                <select class="form-control" id="status" name="status">
                                    <option value="">全部</option>
                                    <#list role_list as item>
                                        <option value="${item.id}">${item.roleName}</option>
                                    </#list>
                                </select>
                            </div>
                            <button type="button" id="search" class="btn btn-outline btn-primary" style="margin-top: 5px">确认</button>
                        </form>
                        <hr>
                        <table class="table table-hover table-center dataTables-example">
                            <thead>
                            <tr>
                                <th>用户ID</th>
                                <th>用户姓名</th>
                                <th>手机号</th>
                                <th>用户类型</th>
                                <th>所属角色</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody id="user_list">
                            </tbody>
                        </table>
                    </div>

                </div>
                <div class="ibox-footer overflow">
                    <h4 class="pull-left">合计：<span id="userCount">0</span>位用户</h4>
                    <div id="copot-page" class="pull-right"></div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    $(document).ready(function () {

        user_list(0);

        $("#search").click(function () {
            user_list(0);
        });
    });

    function Trim(str, is_global) {
        var result;
        result = str.replace(/(^\s+)|(\s+$)/g, "");
        if (is_global.toLowerCase() == "g")
            result = result.replace(/\s/g, "");
        return result;
    }

    function user_list(currentIndex) {
        $("#user_list").empty();
        var nickName = Trim($("#nickName").val(), 'g');
        var mobile = Trim($("#mobile").val(), 'g');
        var status = $("#status option:selected").val();
        $.ajax({
            url: "${baseUrl}/system/user/user_list?pageIndex=" + currentIndex + "&pageSize=" + 10 + "&nickName=" + nickName + "&mobile=" + mobile + "&status=" + status,
            type: "POST",
            success: function (result) {
                var html = "";

                //分页
                new Page({
                    parent: $("#copot-page"),
                    nowPage: currentIndex,
                    pageSize: 10,
                    totalCount: result.user_count,
                    setting: {
                        defaultPage: 7,//默认显示可点击分页的个数
                        firstPageText: "首页",
                        prevPageText: "上一页",
                        nextPageText: "下一页",
                        lastPageText: "尾页"
                    },
                    callback: function (nowPage, totalPage) {
                        user_list(nowPage);
                    }
                });
                $("#userCount").text(result.user_count);
                $.each(result.user_list, function (i, result) {
                    html += "<tr><td>" + result.id + "</td>";
                    html += "<td>" + result.nickName + "</td>";
                    html += "<td>" + result.mobile + "</td>";
                    html += "<td>" + result.userType + "</td>";
                    html += "<td>自健</td>";
                    html += "<td>";
                    html += "<button type='button' onclick='to_edit(" + result.id + ")' class='btn btn-default'><i class='fa fa-pencil'></i> 编辑</button>";
                    html += "<button type='button' onclick='delete_user(" + result.id + ")' class='btn btn-default'><i class='fa fa-trash-o'></i> 删除</button>";
                    html += "</td>";
                    html += "</tr>";
                });
                $("#user_list").html(html);
            }
        });
    }

    function delete_user(id) {
        swal({
            title: "确定删除吗?",
            text: "删除后不可恢复!",
            type: "question",
            showCancelButton: true,
            cancelButtonColor: '#d33',
            cancelButtonText: "容我三思！",
            confirmButtonText: "确定删除!",
        }).then(function () {
            $.ajax({
                url: "user_delete?id=" + id,
                type: "POST",
                success: function (result) {
                    user_list(0);
                    swal("删除成功!",'','success');
                },
                error: function () {
                    swal("删除失败!",'','error');
                }
            });
        }, function () {
        });
    }

    function to_edit(id) {
        window.location.href = 'to_user_edit?id=' + id;
    }
</script>

</@body.page>
</html>